// pages/2411Ayiliao-pages/inpatient/payment.js
Page({
	data: {
		showModal: true,
		rechargeAmount: 6000,
		password: '',
		showDebug: false,
		lastClicked: ''
	},

	onLoad(options) {
		if (options.data) {
			try {
				const data = JSON.parse(decodeURIComponent(options.data))
				this.setData({
					rechargeAmount: data.amount || 6000
				})
			} catch (e) {
				console.error('解析数据失败:', e)
			}
		}
		
		// 在开发环境下显示调试信息
		// #ifdef H5
		this.setData({
			showDebug: true
		})
		// #endif
	},

	closeModal() {
		wx.navigateBack()
	},

	viewBalance() {
		wx.showToast({
			title: '当前余额：¥10000.00',
			icon: 'none',
			duration: 2000
		})
	},

	handleKeyPress(e) {
		const key = e.currentTarget.dataset.key
		console.log('点击按键:', key)
		this.setData({
			lastClicked: key
		})
		
		// 显示点击反馈
		wx.showToast({
			title: `点击了 ${key}`,
			icon: 'none',
			duration: 500
		})
		
		if (key === 'delete') {
			if (this.data.password.length > 0) {
				this.setData({
					password: this.data.password.slice(0, -1)
				})
				console.log('删除密码，当前长度:', this.data.password.length)
			}
		} else if (this.data.password.length < 6) {
			this.setData({
				password: this.data.password + key
			})
			console.log('输入密码:', key, '当前密码:', this.data.password)
			
			// 密码输入完成
			if (this.data.password.length === 6) {
				console.log('密码输入完成，开始支付')
				this.processPayment()
			}
		}
	},

	toggleDebug() {
		this.setData({
			showDebug: !this.data.showDebug
		})
	},

	processPayment() {
		wx.showLoading({
			title: '支付中...'
		})
		
		// 模拟支付过程
		setTimeout(() => {
			wx.hideLoading()
			
			// 跳转到支付成功页面
			const successData = {
				amount: this.data.rechargeAmount,
				hospital: '北京第一人民医院'
			}
			wx.redirectTo({
				url: `/pages/2411Ayiliao-pages/inpatient/payment-success?data=${encodeURIComponent(JSON.stringify(successData))}`
			})
		}, 2000)
	}
}) 